using System;
using System.Collections.Generic;
using UnityEngine;

public class GlobalEventManager : MonoBehaviour
{
    private static Dictionary<string, Delegate> eventMap = new Dictionary<string, Delegate>();

    public static void OnEvent(string eventName, Action listener)
    {
        if (eventMap.ContainsKey(eventName))
        {
            eventMap[eventName] = (Action)eventMap[eventName] + listener;
        }
        else
        {
            eventMap.Add(eventName, listener);
        }
    }

    public static void OnEvent<T>(string eventName, Action<T> listener)
    {
        if (eventMap.ContainsKey(eventName))
        {
            eventMap[eventName] = (Action<T>)eventMap[eventName] + listener;
        }
        else
        {
            eventMap.Add(eventName, listener);
        }
    }

    public static void OffEvent(string eventName, Action listener)
    {
        if (eventMap.ContainsKey(eventName))
        {
            eventMap[eventName] = (Action)eventMap[eventName] - listener;
        }
    }

    public static void OffEvent<T>(string eventName, Action<T> listener)
    {
        if (eventMap.ContainsKey(eventName))
        {
            eventMap[eventName] = (Action<T>)eventMap[eventName] - listener;
        }
    }

    public static void EmitEvent(string eventName)
    {
        if (eventMap.ContainsKey(eventName))
        {
            (eventMap[eventName] as Action)?.Invoke();
        }
    }

    public static void EmitEvent<T>(string eventName, T param)
    {
        if (eventMap.ContainsKey(eventName))
        {
            (eventMap[eventName] as Action<T>)?.Invoke(param);
        }
    }
}
